package com.t2t.top;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.util.BitSet;
import java.util.Set;

@Service
public class CacheServiceImpl {

    Logger log = LoggerFactory.getLogger(CacheServiceImpl.class);

    // 操作redis客户端
    private JedisPool pool;

    @Resource(name = "jedisConnectionFactory")
    private JedisConnectionFactory jedisConnectionFactory;

    /**
     * 获取一个jedis 客户端
     */
    private Jedis getJedis() {
        if (pool == null) {
            pool = new JedisPool(jedisConnectionFactory.getPoolConfig(), jedisConnectionFactory.getHostName(), jedisConnectionFactory.getPort());
        }
        return pool.getResource();
    }

    public void close(Jedis jedis) {
        if (jedis != null) {
            pool.returnResource(jedis);
        }
    }

    public Long hdel(String key, String... field) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.hdel(key, field);
        } finally {
            close(jedis);
        }
    }

    public String set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.set(key, value);
        } finally {
            close(jedis);
        }
    }

    public Long sadd(final String key, final String... members) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.sadd(key, members);
        } finally {
            close(jedis);
        }
    }

    public Long scard(final String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.scard(key);
        } finally {
            close(jedis);
        }
    }

    public Set<String> sdiff(final String... keys) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.sdiff(keys);
        } finally {
            close(jedis);
        }
    }

    public Set<String> smembers(String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.smembers(key);
        } finally {
            close(jedis);
        }
    }

    public String mset(final String... keysvalues) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.mset(keysvalues);
        } finally {
            close(jedis);
        }
    }

    public Long bitcount(String key, long start, long end) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.bitcount(key, start, end);
        } finally {
            close(jedis);
        }
    }

    public Long bitcount(String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();

            return jedis.bitcount(key);
        } finally {
            close(jedis);
        }
    }

    public Boolean setbit(String key, long offset, boolean value) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.setbit(key, offset, value);
        } finally {
            close(jedis);
        }
    }

    public int test(String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            BitSet users = BitSet.valueOf(jedis.get(key.getBytes()));
            return users.cardinality();
        } finally {
            close(jedis);
        }
    }
}
